import time
import pytest
import allure
import TestCase
import random

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_05: 选择1-3个维保项目时提交预约按钮可以点击')
@allure.issue(
    'https://www.tapd.cn/54441127/sparrow/tcase/view/1154441127001066037?url_cache_key=5b07a59231afaf505fb7dc2f4205dfae')
class TestCaseService05:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 车服务
        self.service_index_page = CommonPage(self.driver).get_service_index_page()
        self.service_city_page = CommonPage(self.driver).get_service_city_page()
        self.service_customer_hotline_page = CommonPage(self.driver).get_service_customer_hotline_page()
        self.service_driving_score_page = CommonPage(self.driver).get_service_driving_score_page()
        self.service_health_house_keeper_page = CommonPage(self.driver).get_service_health_house_keeper_page()
        self.service_maintenance_plan_page = CommonPage(self.driver).get_service_maintenance_page()
        self.service_my_reservation_page = CommonPage(self.driver).get_service_my_reservation_page()
        self.service_new_operating_page = CommonPage(self.driver).get_service_new_operating_page()
        self.service_reservation_page = CommonPage(self.driver).get_service_reservation_page()
        self.service_store_select_page = CommonPage(self.driver).get_service_store_select_page()
        self.service_time_page = CommonPage(self.driver).get_service_time_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('service')
        # XiaoMaUtils.clear_data('personal')
        self.driver.quit()

    @allure.story('step1:点击应用页,进入车辆管家')
    @pytest.mark.run(order=51)
    def test_service_05_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.SERVICE)
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 查看是否进入车服务,点击去维保按钮,点击门店")
    @pytest.mark.run(order=52)
    def test_service_05_2_click_service(self):
        try:
            assert self.service_index_page.is_display_service_index()
            self.service_index_page.click_maintain()
            self.service_reservation_page.is_display_service_reservation()
            self.service_reservation_page.click_service_hint()
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step3: 点击第一个门店,勾选后选择确认")
    @pytest.mark.run(order=53)
    def test_service_05_3_click_service(self):
        try:
            self.service_store_select_page.is_display_service_store()
            a = self.service_store_select_page.is_display_service_no_store()
            if a == True:
                print("当前位置没有获取城市,无法获取gps,请移动到开阔位置")
            else:
                b = self.service_store_select_page.get_element_attribute(
                    self.service_store_select_page.get_confirm_button_enabled(), "enabled")
                if b == "false":
                    self.service_store_select_page.click_list_select()
                    self.service_store_select_page.click_confirm_button()
                    self.service_reservation_page.is_display_service_reservation()
                else:
                    print("尚未选择门店,请重新选择")
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step4: 点击到店时间,选择到店时间后点击确认")
    @pytest.mark.run(order=54)
    def test_service_05_4_click_service(self):
        try:
            self.service_reservation_page.click_arrival_hint_time()
            self.service_time_page.is_display_service_time()
            self.service_time_page.click_reservation()
            self.service_time_page.click_submit()
            self.service_reservation_page.is_display_service_reservation()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step5: 点击维保项目1个或者多个,点击提交按钮")
    @pytest.mark.run(order=55)
    def test_service_05_5_click_service(self):
        try:
            a = self.service_reservation_page.get_element_attribute(self.service_reservation_page.get_submit(),
                                                                    "enabled")
            print(a)
            if a == 'true':
                print("状态错误,请重新点击")
            else:
                self.service_reservation_page.click_select_project()
                n = random.randint(0, 2)
                if n == 0:
                    self.service_reservation_page.click_appointment_maintenance()
                    self.service_reservation_page.click_sure()
                elif n == 1:
                    self.service_reservation_page.click_appointment_maintenance()
                    self.service_reservation_page.click_appointment_maintenance(index=2)
                    self.service_reservation_page.click_sure()
                else:
                    self.service_reservation_page.click_appointment_maintenance()
                    self.service_reservation_page.click_appointment_maintenance(index=2)
                    self.service_reservation_page.click_appointment_maintenance(index=3)
                    self.service_reservation_page.click_sure()
                self.service_reservation_page.click_submit()
                self.service_reservation_page.is_toast_service_reservation()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_service_05.py")
